package Exchanger_15;
import java.util.concurrent.Exchanger;
public class 生产者_消费者交换缓冲区 {
    static class Buffer {
        String[] items;
        int count;

        Buffer(int size) {
            items = new String[size];
        }
    }

    public static void main(String[] args) {
        final Exchanger<Buffer> exchanger = new Exchanger<Buffer>();
        final int BUFFER_SIZE = 3;
        final int PRODUCER_COUNT = 2;
        final int CONSUMER_COUNT = 2;
        final int ITEMS_PER_PRODUCER = 6;

        // 生产者
        for (int i = 0; i < PRODUCER_COUNT; i++) {
            final int producerId = i;
            new Thread(new Runnable() {
                public void run() {
                    Buffer currentBuffer = new Buffer(BUFFER_SIZE);
                    try {
                        for (int j = 0; j < ITEMS_PER_PRODUCER; j++) {
                            // 填充缓冲区
                            if (currentBuffer.count == BUFFER_SIZE) {
                                System.out.println("生产者" + producerId + " 缓冲区已满，等待交换");
                                currentBuffer = exchanger.exchange(currentBuffer);
                                System.out.println("生产者" + producerId + " 获得空缓冲区");
                            }

                            String item = "产品-" + producerId + "-" + j;
                            currentBuffer.items[currentBuffer.count++] = item;
                            System.out.println("生产者" + producerId + " 生产: " + item);
                            Thread.sleep((long)(Math.random() * 200)); // 模拟生产时间
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }, "生产者-" + i).start();
        }

        // 消费者
        for (int i = 0; i < CONSUMER_COUNT; i++) {
            final int consumerId = i;
            new Thread(new Runnable() {
                public void run() {
                    Buffer currentBuffer = new Buffer(BUFFER_SIZE);
                    currentBuffer.count = 0; // 初始为空缓冲区
                    try {
                        while (true) {
                            // 消费缓冲区
                            if (currentBuffer.count == 0) {
                                System.out.println("消费者" + consumerId + " 缓冲区为空，等待交换");
                                currentBuffer = exchanger.exchange(currentBuffer);
                                System.out.println("消费者" + consumerId + " 获得满缓冲区");
                            }

                            String item = currentBuffer.items[--currentBuffer.count];
                            System.out.println("消费者" + consumerId + " 消费: " + item);
                            Thread.sleep((long)(Math.random() * 300)); // 模拟消费时间
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }, "消费者-" + i).start();
        }
    }
}